package com.lagou.edu.controller;

import com.lagou.edu.pojo.LagouAuthCode;
import com.lagou.edu.service.LagouAutoCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mail")
public class MailController {


    @Autowired
    private LagouAutoCodeService lagouAutoCodeService;


    @Value("${server.port}")
    private Integer port;

    //发送验证码
    @GetMapping("/sendVerificationCode/{mail}")
    public Integer sendMail(@PathVariable String mail) throws Exception {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();

        StringBuffer verificationCode = new StringBuffer();
        for (int i =0 ;i<6;i++) {
            int s = (int) (Math.random() * 10);
            System.out.println(s);
            verificationCode.append(s);
        }

        Date now = new Date();

        lagouAuthCode.setEmail(mail);
        lagouAuthCode.setCode(verificationCode.toString());
        lagouAuthCode.setCreatetime(now);
        lagouAuthCode.setExpiretime(new Date(now.getTime() + 600000));

        lagouAutoCodeService.addLagouAuthCode(lagouAuthCode);

        System.out.println("====>>>>>>>>>>>>>>我是8001，验证码已经发送......");

        return port;
    }



    //查找验证码
    @GetMapping("/findAuthCodeByEmail/{mail}")
    public List<LagouAuthCode> findAuthCodeByEmail(@PathVariable String mail) {
        return lagouAutoCodeService.findAuthCodeByEmail(mail);
    }
}